<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>MyEvent</title>
</head>
<body>
<div id="app">
    <div class="parent-content">
        <h1>{{messages }}</h1>
    </div>
    <higirl></higirl>
</div>
<template id="template-higirl">
    <div class="child-content">
        <h2>新茶上市</h2>
        Girl：<input v-model="girl">
        <button @click="newGirl">品新茶</button>
    </div>
</template>
<script src="js/vue.js"></script>
<script>
    Vue.component('higirl', {
        template: '#template-higirl',
        data: function () {
            return {
                girl: ''
            }
        },
        methods: {
            newGirl: function () {
                if (this.girl.trim()) {
                    // this.$dispatch('new-girl',this.girl)
                    eventHub.$emit('new-girl', this.girl)
                    this.girl = ''
                }
            }
        }
    })

    var eventHub = new Vue();
    new Vue({
        el: "#app",
        data: {
            messages: [],

        },
        created: function () {
            eventHub.$on('new-girl', this.addNewGirl)
        },
        methods: {
            addNewGirl: function (girl) {
                this.messages.push(girl)
            }
        }
    })


</script>
</body>
</html>
